<template>
  <div>
    <el-form :inline="true">
      <el-form-item>
        <el-input v-model="query.keyWord" placeholder="姓名/电话号" clearable style="width: 100%"></el-input>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="query.status"
          clearable
          placeholder="请选择审核状态">
          <el-option
            v-for="item in auditStatusList"
            :key="item.value"
            :label="item.name"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          v-if="hasPermission(['system:student:list'])"
          type="primary" @click="searchStudent" icon="el-icon-search">查询
        </el-button>
        <el-button
          v-if="hasPermission(['system:student:save'])"
          icon="el-icon-circle-plus" @click="addStudent">新增
        </el-button>
      </el-form-item>

      <!--      <el-form-item v-if="hasPermission(['system:student:import'])">
              <el-upload
                :headers="headers"
                class="upload-demo"
                :before-upload="beforeUploadStudentInfo"
                :with-credentials="true"
                :on-success="uploadStudentInfoSuccess"
                :action="uploadAction"
                :show-file-list="false">
                <el-button  icon="el-icon-download" type="primary">导入</el-button>
              </el-upload>
            </el-form-item>

            <el-form-item>
              <el-button @click="downTemplate" type="success">下载模板</el-button>
            </el-form-item>-->

      <el-form-item>
        <el-button
          v-if="hasPermission(['system:studentInfo:export'])"
          @click="exportStudent" icon="el-icon-upload2" type="primary" plain>导出
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="studentList"
      border
      v-loading="loading"
      style="width: 100%;">

      <el-table-column
        prop="name"
        width="100"
        align="center"
        label="姓名">
      </el-table-column>
      <el-table-column
        prop="loginName"
        width="100"
        align="center"
        label="账号">
      </el-table-column>
      <el-table-column
        prop="entName"
        width="180"
        align="center"
        label="所属企业">
      </el-table-column>

      <el-table-column
        align="center"
        width="80"
        label="职工头像">
        <template slot-scope="scope">
          <img v-if="scope.row.headImg" style="width: 40px; height: 40px;" :src="scope.row.headImg"/>
        </template>
      </el-table-column>

      <el-table-column
        prop="sex"
        width="60"
        align="center"
        label="性别">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.sex === 1" size="small">男</el-tag>
          <el-tag v-else size="small">女</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        prop="age"
        width="50"
        align="center"
        label="年龄">
      </el-table-column>

      <el-table-column
        prop="status"
        align="center"
        width="100"
        label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" type="info" size="small">{{
              auditStatusDict[scope.row.status]
            }}
          </el-tag>
          <el-tag v-else-if="scope.row.status === 2" type="danger" size="small">{{
              auditStatusDict[scope.row.status]
            }}
          </el-tag>
          <el-tag v-else size="small">{{ auditStatusDict[scope.row.status] }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        prop="createDate"
        align="center"
        width="140"
        label="创建时间">
      </el-table-column>

      <el-table-column
        header-align="center"
        align="left"
        min-width="240"
        label="操作">
        <template slot-scope="scope">
          <el-button
            size="small"
            v-if="hasPermission(['system:student:updatePassword'])"
            @click="updateStudentAccount(scope.row)"
            type="warning">重置密码
          </el-button>
          <el-button
            size="small"
            v-if="hasPermission(['system:student:update'])"
            @click="updateStudent(scope.row)"
            type="warning">修改
          </el-button>
          <el-button
            size="small"
            v-if="hasPermission(['system:student:deleteById'])"
            @click="deleteById(scope.row)"
            type="danger">删除
          </el-button>
          <el-button
            size="small"
            v-if="hasPermission(['system:student:audit']) && scope.row.status === 1"
            @click="openAuditDialog(scope.row)"
            type="primary">审核
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      title="修改密码"
      :visible.sync="passWordFormVisible"
      :append-to-body="true">
      <el-form :model="passwordForm" :rules="dataRule" ref="passwordForm" label-width="80px">
        <el-form-item label="新密码" prop="password">
          <el-input type="password" v-model="passwordForm.password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input type="password" v-model="passwordForm.confirmPassword"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button @click="passWordFormVisible = false">取消</el-button>
      <el-button type="primary" @click="updateAccountPassword()">确定</el-button>
    </span>
    </el-dialog>

    <el-pagination
      background
      @size-change="sizeChangeHandler"
      @current-change="currentChangeHandler"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      :total="totalCount"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <student-form
      :showFlag="formDialog"
      @closeDialog="closeDialog"
      @close="closeForm"
      :formData="studentForm">
    </student-form>

    <el-dialog
      title="企业审核"
      :close-on-click-modal="false"
      :visible.sync="showAuditDialog">
      <el-form :model="auditForm" :rules="auditRules" ref="auditForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="职工名称:">
          <span>{{ auditForm.name }}</span>
        </el-form-item>
        <el-form-item label="所属公司:">
          <span>{{ auditForm.entName }}</span>
        </el-form-item>
        <el-form-item label="审核描述:" prop="desc">
          <textarea :rows="4" style="width: 100%" v-model="auditForm.desc"></textarea>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showAuditDialog = false">取消</el-button>
        <el-button type="danger" @click="audit(false)">审核不通过</el-button>
        <el-button type="primary" @click="audit(true)">审核通过</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {auditStatusDict, auditStatusList} from '@/api/dict'
import studentForm from '@/components/education/student-form'

let thisPage = null
export default {
  name: 'student',
  components: {studentForm},
  data () {
    let validateConfirmPassword = (rule, value, callback) => {
      if (this.passwordForm.newPassword !== value) {
        callback(new Error('确认密码与新密码不一致'))
      } else {
        callback()
      }
    }
    return {
      showAuditDialog: false,
      auditStatusDict: auditStatusDict,
      auditStatusList: auditStatusList,
      formDialog: false,
      studentList: [],
      fileUrl: this.$http.getFileHost(),
      query: {
        keyWord: '',
        status: null
      },
      headers: {
        Authorization: localStorage.getItem('Authorization'),
        Platform: 'educationAdmin'
      },
      dataRule: {
        password: [
          {required: true, message: '新密码不能为空', trigger: 'blur'}
        ],
        confirmPassword: [
          {required: true, message: '确认密码不能为空', trigger: 'blur'},
          {validator: validateConfirmPassword, trigger: 'blur'}
        ]
      },
      passWordFormVisible: false,
      excelLoading: false,
      studentForm: {
        name: '',
        disabledFlag: 0,
        age: '',
        sex: '',
        loginName: '',
        password: '',
        confirmPassword: '',
        headImg: '',
        address: '',
        mobile: '',
        fatherName: '',
        motherName: ''
      },
      passwordForm: {
        password: '',
        confirmPassword: ''
      },
      loading: true,
      currentPage: 1,
      pageSize: 10,
      totalCount: 0,
      uploadAction: this.$http.httpUrl('/system/student/importStudent'),
      auditForm: {
        desc: '',
        id: null,
        name: '',
        entName: '',
        passed: true
      },
      auditRules: {
        desc: [{max: 255, message: '不能超过255个字符', trigger: 'blur'}]
      }
    }
  },

  beforeCreate () {
    thisPage = this
  },

  mounted () {
    this.getStudentList()
  },

  methods: {
    openAuditDialog (row) {
      this.auditForm.id = row.id
      this.auditForm.name = row.name
      this.auditForm.entName = row.entName
      this.showAuditDialog = true
    },
    audit (passed) {
      this.auditForm.passed = passed
      this.$confirm(`确定审核${passed ? '' : '不'}通过？, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$refs['auditForm'].validate((valid) => {
          if (valid) {
            this.axios.post(this.$http.httpUrl('/system/student/audit'), this.auditForm)
              .then((response) => {
                this.showAuditDialog = false
                if (response.data.code === 1) {
                  this.$message({
                    type: 'success',
                    message: '审核成功!'
                  })
                  this.getStudentList()
                }
              })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    updateAccountPassword () {
      this.axios.post(this.$http.httpUrl('/system/student/updatePassword'), this.passwordForm)
        .then(response => {
          this.passWordFormVisible = false
          if (response.data.code === 1) {
            this.$message({
              type: 'success',
              message: response.data.message
            })
          } else {
            this.$message({
              type: 'error',
              message: response.data.message
            })
          }
        })
    },

    updateStudentAccount (item) {
      this.passwordForm.id = item.id
      this.passwordForm.encrypt = item.encrypt
      this.passwordForm.password = ''
      this.passwordForm.confirmPassword = ''
      this.passWordFormVisible = true
    },

    sizeChangeHandler (val) {
      this.pageSize = val
      this.currentPage = 1
      this.getStudentList()
    },

    currentChangeHandler (val) {
      this.currentPage = val
      this.getStudentList()
    },

    addStudent () {
      this.formDialog = true
      this.clearFormData()
    },

    beforeUploadStudentInfo () {
      this.excelLoading = this.$loading({
        lock: true,
        text: '数据导入中, 请等待',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      return true
    },

    downTemplate () {
      //  let name = encodeURI('学生信息导入模板')
      // location.href = this.$http.getFileHost() + '/excel/' + name + '.xlsx'

      location.href = this.$http.httpUrl('/static/template/studentImport.xlsx')
    },

    uploadStudentInfoSuccess (res, file) {
      this.excelLoading.close()
      if (res.code === 1) {
        this.$message.success(res.message)
        this.getStudentList()
      } else if (res.code === 3) {
        this.$message.error(res.message)
        location.href = this.$http.getFileHost() + res.data
      } else {
        this.$message.error(res.message)
      }
    },

    clearFormData () {
      this.studentForm = {
        name: '',
        disabledFlag: 0,
        age: '',
        sex: '',
        loginName: '',
        password: '',
        confirmPassword: '',
        headImg: '',
        address: '',
        mobile: '',
        fatherName: '',
        motherName: ''
      }
    },

    deleteById (item) {
      let that = this
      that.$confirm('确定删除职工' + item.name + '?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        that.axios.delete(that.$http.httpUrl('/system/student/' + item.id)).then(function (response) {
          if (response.data.code === 1) {
            that.$message({
              type: 'success',
              message: response.data.message
            })
            that.getStudentList()
          }
        })
      })
    },

    // 导出职工
    exportStudent () {
      let that = this
      let params = {
        keyWord: that.query.keyWord,
        gradeType: that.query.grade_type
      }
      that.axios.post(that.$http.httpUrl('/system/studentInfo/exportStudent'), params).then(function (response) {
        location.href = that.$http.getFileHost() + '/' + response.data.message
      })
    },

    closeForm () {
      this.formDialog = false
      this.getStudentList()
      this.clearFormData()
    },

    closeDialog () {
      this.formDialog = false
    },

    updateStudent (item) {
      this.studentForm = {
        id: item.id,
        disabledFlag: item.disabledFlag ? 1 : 0,
        loginName: item.loginName,
        name: item.name,
        age: item.age,
        sex: item.sex,
        headImg: item.headImg,
        address: item.address,
        mobile: item.mobile,
        fatherName: item.fatherName,
        motherName: item.motherName,
        post: item.post,
        email: item.email,
        status: item.status,
        enterpriseId: item.enterpriseId,
        isAdmin: item.isAdmin
      }
      this.formDialog = true
    },

    searchStudent () {
      this.currentPage = 1
      this.loading = true
      this.getStudentList()
    },

    getStudentList () {
      this.axios.get(this.$http.httpUrl('/system/student'), {
        params: {
          pageNumber: this.currentPage,
          pageSize: this.pageSize,
          name: this.query.keyWord,
          status: this.query.status
        }
      }).then(response => {
        this.studentList = response.data.data.dataList
        this.totalCount = response.data.data.total
        this.loading = false
      })
    }
  },

  filters: {
    getGradeName (gradeType) {
      for (let i = 0; i < thisPage.gradeTypeList.length; i++) {
        if (thisPage.gradeTypeList[i].code === gradeType) {
          return thisPage.gradeTypeList[i].value
        }
      }
    }
  }
}
</script>
